package com.gorkr;

public class L1143 {
    public int longestCommonSubsequence(String text1, String text2) {
        int m = text1.length();
        int n = text2.length();
        char[] c1 = text1.toCharArray();
        char[] c2 = text2.toCharArray();
        int[][] dp = new int[m+1][n+1]; // dp定义：dp[i][j] text1[0...i] text2[0..j]最长子序列

        // base case dp[0][...] = dp[...][0] = 0
        // 状态
        for (int i = 1; i <=m ; i++) {
            for(int j = 1; j<=n; j++){
                // 状态转移
                if(c1[i-1]==c2[j-1]){
                    dp[i][j] = dp[i-1][j-1] + 1;
                }else {
                    dp[i][j] = Math.max(dp[i][j-1], dp[i-1][j]);

                }
            }

        }
        return dp[m][n];

    }
}

